package cn.itcast.web.controller.company;

import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.company.Company;
import cn.itcast.service.company.CompanyService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/company", name = "企业管理")
public class CompanyController extends BaseController {

    @Reference
    private CompanyService companyService;



    @RequestMapping(value = "/list", name = "查询企业")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize) {

        PageInfo page =  companyService.findByPageHelper(pageNum,pageSize);
        request.setAttribute("page", page);

        return "company/company-list";
    }
    /*@RequestMapping(value = "/list", name = "查询企业")
    public String list(HttpServletRequest request) {
        List<Company> list = companyService.findAll();
        request.setAttribute("list", list);
        return "company/company-list";
    }*/

    @RequestMapping(value = "/toAdd", name = "新增页面")
    public String toAdd(){
        return "company/company-add";
    }

    @RequestMapping(value = "/edit",name = "新增/修改")
    public String edit(Company company){
        if (StrUtil.hasEmpty(company.getId())) {
            companyService.save(company);
        } else {
            companyService.update(company);
        }
        return "redirect:/company/list.do";
    }


    @RequestMapping(value = "/toUpdate",name = "修改页面")
    public String toUpdate(String id,HttpServletRequest request){
        Company company = companyService.findById(id);
        request.setAttribute("company", company);
        return "company/company-update";
    }



    @RequestMapping(value = "/delete", name = "删除")
    public String delete(String id) {
        companyService.delete(id);
        return "redirect:/company/list.do";
    }
}
